//
//  LuckyResultVC.m
//  SMTools
//
//  Created by fengsh on 2019/1/1.
//  Copyright © 2019 fengsh. All rights reserved.
//

#import "LuckyResultVC.h"
#import "LuckyApi.h"
#import "LuckyUtils.h"
#import <SDWebImage/SDWebImageManager.h>

@interface LuckyResultVC ()<NSTableViewDelegate,NSTableViewDataSource>
@property (weak) IBOutlet       NSTableView                         *tabresults;
@property (nonatomic, strong)   NSMutableArray                      *list;
@end

@implementation LuckyResultVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.list = [NSMutableArray array];
    // Do view setup here.
    [self refreshData];
}

- (void)refreshData
{
    [[LuckyApi api]lucky_api_post_queryResultList:@{} comletion:^(id respobj, NSError *error) {
        if (!error) {
            NSString *retcode = respobj[@"retcode"];
            if ([retcode isEqualToString:@"00000"]) {
                [self.list removeAllObjects];
                NSArray *datas = respobj[@"data"];
                if (datas) {
                    [self.list addObjectsFromArray:datas];
                }
                
                [self.tabresults reloadData];
            }
        }
    }];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return self.list.count;
}

- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
    return 50;
}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSUserInterfaceItemIdentifier idf = tableColumn.identifier;
    NSTableCellView *cv = [tableView makeViewWithIdentifier:idf owner:nil];
    NSInteger colidx = [tableView columnWithIdentifier:idf];
    NSDictionary *obj = self.list[row];

    if (colidx == 0) {
        NSString *photo = obj[@"photo"];
        if (photo.length > 0) {
            NSURL *av = [NSURL URLWithString:photo];
            [[SDWebImageManager sharedManager]loadImageWithURL:av options:SDWebImageRetryFailed progress:nil completed:^(NSImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
                NSImageView *imgv = cv.subviews[0];
                imgv.image = image;
            }];
        }
        
        return cv;
    }
    
    switch (colidx) {
        case 1:
        {
            NSString *mb = [LuckyUtils lucky_dictionaryvalue:obj[@"employeeNo"]];
            [cv.textField setStringValue:mb?:@""];
            tableColumn.hidden = YES;
        }
            break;
        case 2:
        {
            NSString *mb = [LuckyUtils lucky_dictionaryvalue:obj[@"name"]];
            [cv.textField setStringValue:mb?:@""];
        }
            break;
        case 3:
        {
            NSString *mb = [LuckyUtils lucky_dictionaryvalue:obj[@"phone"]];
            [cv.textField setStringValue:mb?:@""];
        }
            break;
        case 4:
        {
            NSString *mb = [LuckyUtils lucky_dictionaryvalue:obj[@"prizeLevelName"]];
            [cv.textField setStringValue:mb?:@""];
        }
            break;
        case 5:
        {
            NSString *mb = [LuckyUtils lucky_dictionaryvalue:obj[@"email"]];
            [cv.textField setStringValue:mb?:@""];
        }
            break;
        case 6:
        {
            NSString *mb = [LuckyUtils lucky_dictionaryvalue:obj[@"department"]];
            [cv.textField setStringValue:mb?:@""];
        }
            break;
        case 7:
        {
            for (NSButton *button in cv.subviews) {
                
                if ([button.identifier isEqualToString:@"btn_sender"]) {
                    [button setAction:@selector(onBtnSender:)];
                }
                [button setTag:row];
                [button  setTarget:self];
                
            }
            
        }
            break;
        default:
            break;
    }

    
    return cv;
}

- (void)tableView:(NSTableView *)tableView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row
{
    if (row % 2 != 0) {
        rowView.backgroundColor = [NSColor windowBackgroundColor];
    }
}

- (IBAction)onRefreshClicked:(id)sender
{
    [self refreshData];
}

- (void)onBtnSender:(NSButton *)btn
{
    NSInteger idx = btn.tag;
    NSDictionary *obj = self.list[idx];
    long pid = [obj[@"id"]longLongValue];
    [[LuckyApi api]lucky_api_post_send:@{@"id":@(pid)} comletion:^(id respobj, NSError *error) {
        
    }];
}

@end
